#!/bin/sh

# Slackware build script for wine

# Copyright 2016, 2018-2019 orbea
# All rights reserved.

# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

PRGNAM=wine
VERSION=${VERSION:-4.15}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
else
  SLKCFLAGS="-O2"
fi

set -eu

# Selects the default wine.
# $1 = WIN32
# $2 = WIN64
wine_arch () {
  if [ "${WIN32:=$1}" != no ]; then
    WIN32=yes
  fi

  if [ "${WIN64:=$2}" != no ]; then
    WIN64=yes
  fi
}

if [ "$ARCH" = x86_64 ]; then
  if [ -f /usr/lib/libgcc_s.so ]; then
    wine_arch yes yes
  else
    wine_arch no yes
  fi
else
  wine_arch yes no
fi

if [ "$WIN32" != yes ] && [ "$WIN64" != yes ]; then
  printf %s\\n 'ERROR: Both 32 and 64 bit builds are disabled.' >&2
  exit 1
fi

if [ "$ARCH" != x86_64 ] && [ "$WIN64" = yes ]; then
  printf %s\\n "ERROR: 64 bit builds are not supported on '$ARCH'." >&2
  exit 1
fi

if [ "$WIN32" = yes ] && [ "$WIN64" = yes ]; then
  PKGNAM=$PRGNAM
elif [ "$WIN32" = yes ]; then
  PKGNAM=${PRGNAM}32
elif [ "$WIN64" = yes ]; then
  PKGNAM=${PRGNAM}64
fi

[ "${STAGING:=no}" != no ] && PKGNAM=$PKGNAM-staging

if [ "${DEBUG:=no}" != no ]; then
  PKGNAM=$PKGNAM-debug
  SLKCFLAGS="$(printf %s "$SLKCFLAGS" | sed 's/-O2/-O0 -g/')"
fi

PKG=$TMP/package-$PKGNAM

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PKGNAM-$VERSION
mkdir $PKGNAM-$VERSION
cd $PKGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.xz
[ "$STAGING" != no ] &&
  tar xvf $CWD/$PRGNAM-staging-$VERSION.tar.gz
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \+ -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \+

cd $PRGNAM-$VERSION

WINE_FLAGS=" \
  --prefix=/opt/$PKGNAM/$VERSION \
  --mandir=/opt/$PKGNAM/$VERSION/man \
  --docdir=/usr/doc/$PKGNAM-$VERSION \
  --with-gnutls=yes \
  --build=$ARCH-slackware-linux"

[ "${OPENGL:-yes}" = yes ] && WINE_FLAGS="${WINE_FLAGS} --with-opengl"
[ "${VULKAN:-yes}" = yes ] && WINE_FLAGS="${WINE_FLAGS} --with-vulkan"

[ "${CUPS:-no}" = no ] && WINE_FLAGS="${WINE_FLAGS} --without-cups"
[ "${DBUS:-no}" = no ] && WINE_FLAGS="${WINE_FLAGS} --without-dbus"
[ "${GSTREAMER:-no}" = no ] && WINE_FLAGS="${WINE_FLAGS} --without-gstreamer"

if [ "$STAGING" != no ]; then
  [ "${GTK3:-no}" = no ] && WINE_FLAGS="${WINE_FLAGS} --without-gtk3"

  # Apply Staging patches
  (
    cd $TMP/$PKGNAM-$VERSION/$PRGNAM-staging-$VERSION/patches

    ./patchinstall.sh --all \
      DESTDIR=$TMP/$PKGNAM-$VERSION/$PRGNAM-$VERSION
  )
fi

if [ "$ARCH" = x86_64 ] && [ "$WIN32" = yes ]; then
  # TODO: Need a 32-bit clang
  export CC=gcc
  export CXX=g++
fi

autoreconf -fi

if [ "$WIN64" = yes ]; then
  mkdir ${PRGNAM}64-build

  (
    cd ${PRGNAM}64-build

    set -- $(printf %s "$WINE_FLAGS")

    CFLAGS="$SLKCFLAGS" \
    CXXFLAGS="$SLKCFLAGS" \
    ../configure \
      --libdir=/opt/$PKGNAM/$VERSION/lib64 \
      --enable-win64 \
      "$@"

    make
    make install DESTDIR=$PKG
  )

  WINE_FLAGS="${WINE_FLAGS} --with-wine64=../${PRGNAM}64-build"
fi

if [ "$WIN32" = yes ]; then
  mkdir $PRGNAM-build

  (
    cd $PRGNAM-build

    set -- $(printf %s "$WINE_FLAGS")

    CFLAGS="$SLKCFLAGS" \
    CXXFLAGS="$SLKCFLAGS" \
    ../configure \
      --libdir=/opt/$PKGNAM/$VERSION/lib \
      "$@"

    make
    make install DESTDIR=$PKG
  )
fi

if [ "$DEBUG" = no ]; then
  find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" |
    grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true
fi

find $PKG/opt/$PKGNAM/$VERSION/man -type f -exec gzip -9 {} \;
for i in $( find $PKG/opt/$PKGNAM/$VERSION/man -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done

mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -a ANNOUNCE AUTHORS COPYING.* LICENSE* MAINTAINERS README VERSION \
  $PKG/usr/doc/$PKGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PKGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

sed -e "s/    |-----handy-/$(printf "%${#PKGNAM}s\\n")|-----handy-/" \
    -e "s/$PRGNAM: $PRGNAM/$PKGNAM: $PKGNAM/" \
    -e "s/$PRGNAM:/$PKGNAM:/" \
    -i $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PKGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
